<?php
namespace Crm\Controller;

use Zend\View\Model\ViewModel;

/**
 * 溯源材料
 */
class MaterialController extends AbstractController
{

    public function listAction()
    {
        $cate = $this->getMaterialCategoryTable()->findAll();
        return new ViewModel(array(
            'cate' => $cate
        ));
    }

    public function ajaxMaterialListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        (int) $material_category_id = isset($request['material_category_id']) ? $request['material_category_id'] : 0;
        $material_name = isset($request['material_name']) ? $request['material_name'] : 0;
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'material_category_id' => $material_category_id,
            'material_name' => $material_name,
            'page' => $page
        );
        
        $paginator = $this->getMaterialTable()->find($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }

    public function addAction()
    {
        $cate = $this->getMaterialCategoryTable()->findAll();
        $product = $this->getProductTable()->findAll();
        return new ViewModel(array(
            'cate' => $cate,
            'product' => $product
        ));
    }

    /**
     * 材料入库
     */
    public function insertAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $query = $this->request->getPost()->toArray();
            
            
            $query1 = array(
                'file' => ''
            );
            $mate_data = array_diff_key($query, $query1);
            
            //判断材料图片
            $mate_img = array_slice($mate_data, 13);
            //WU_FILE_3
            $mate_res = array_diff_key($mate_data, $mate_img);
            
            $mate_res['material_img'] = json_encode($mate_img);
           
            $result = $this->getMaterialTable()->create($mate_res);
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("保存成功！");
                return $this->redirect()->toRoute('crm/material');
            } else {
                
                $this->flashMessenger()->addErrorMessage("保存失败！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'add'
                ));
            }
        }
    }

    public function editAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['material_id'];
        $material = $this->getMaterialTable()->findOne($id);
        
        $material_img = json_decode($material['material_img'],true);
        
        $cate = $this->getMaterialCategoryTable()->findAll();
        $product = $this->getProductTable()->findAll();
        return new ViewModel(array(
            'cate' => $cate,
            'product' => $product,
            'material' => $material,
            'material_img' => $material_img
        ));
    }

    public function updateAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            
            $data1 = array(
                'material_id' => '',
                'file' => ''
            );
            $mate_data = array_diff_key($data, $data1);
            
            
            
            //判断材料图片
            $mate_img = array_slice($mate_data, 13);
            //WU_FILE_3
            $mate_res = array_diff_key($mate_data, $mate_img);
            
            $mate_res['material_img'] = json_encode($mate_img);
            
       
            
            (int) $id = $data['material_id'];
            
            $result = $this->getMaterialTable()->updateById($mate_res, $id);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'list'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                
                return $this->redirect()->toUrl("list?material_id=$id");
            }
        }
    }

    public function removeAction()
    {
        return new ViewModel();
    }

    public function detailsAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['material_id'];
        $material = $this->getMaterialTable()->findOne($id);
        
        $cate = $this->getMaterialCategoryTable()->findAll();
        $product = $this->getProductTable()->findAll();
        return new ViewModel(array(
            'cate' => $cate,
            'product' => $product,
            'material' => $material
        ));
    }
    
    
    public function cateListAction()
    {
        return new ViewModel();
    }

    public function ajaxMaterialCateListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        (int) $material_category_name = isset($request['material_category_name']) ? $request['material_category_name'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'material_category_name' => $material_category_name,
            'page' => $page
        );
        
        $paginator = $this->getMaterialCategoryTable()->find($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        
        return $viewModel;
    }

    public function catelogListAction()
    {
        $catelog = $this->getMaterialCatalogTable()->findAll();
        return new ViewModel(array(
            'catelog' => $catelog
        ));
    }

    public function ajaxMaterialCatelogListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        (int) $material_catalog_id = isset($request['material_catalog_id']) ? $request['material_catalog_id'] : 0;
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'material_catalog_id' => $material_catalog_id,
            'page' => $page
        );
        
        $paginator = $this->getMaterialCatalogTable()->find($query);
        
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }

    public function addCatelogAction()
    {
        return new ViewModel();
    }

    public function insertCatelogAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $cate_name = $request->getPost("catalog_name");
            
            $data = array(
                'material_catalog_name' => $cate_name
            );
            
            $result = $this->getMaterialCatalogTable()->create($data);
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("新增成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'catelogList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("新增失败！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'addCatelog'
                ));
            }
        }
    }

    public function editCatelogAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['material_catalog_id'];
        $catelog = $this->getMaterialCatalogTable()->findOne($id);
        return new ViewModel(array(
            'catelog' => $catelog
        ));
    }

    public function updateCatelogAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            
            $data1 = array(
                'material_catalog_id' => ''
            );
            $catelog_data = array_diff_key($data, $data1);
            
            (int) $id = $data['material_catalog_id'];
            $result = $this->getMaterialCatalogTable()->updateById($catelog_data, $id);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'catelogList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                
                return $this->redirect()->toUrl("catelogList?material_catalog_id=$id");
            }
        }
    }

    public function addCateAction()
    {
        $catalog = $this->getMaterialCatalogTable()->findAll();
        
        return new ViewModel(array(
            'catalog' => $catalog
        ));
    }

    public function insertCateAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $catelog_id = $request->getPost("catelog_id");
            $cate_name = $request->getPost("cate_name");
            
            $data = array(
                'material_catalog_id' => $catelog_id,
                'material_category_name' => $cate_name
            );
            
            $result = $this->getMaterialCategoryTable()->create($data);
            if ($result) {
 
                
                $this->flashMessenger()->addSuccessMessage("新增成功");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'cateList'
                ));
            } else {

                
                $this->flashMessenger()->addErrorMessage("新增失败");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'addCate'
                ));
            }

        } else {
            
            $this->flashMessenger()->addErrorMessage("非法请求");
            return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'addCate'
                ));
        }
    }

    public function editCateAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['material_category_id'];
        $cate = $this->getMaterialCategoryTable()->findOne($id);
        $catalog = $this->getMaterialCatalogTable()->findAll();
        
        return new ViewModel(array(
            'catalog' => $catalog,
            'cate' => $cate
        ));
    }

    public function updateCateAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            
            $data1 = array(
                'material_category_id' => ''
            );
            $cate_data = array_diff_key($data, $data1);
            
            (int) $id = $data['material_category_id'];
            $result = $this->getMaterialCategoryTable()->updateById($cate_data, $id);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'cateList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                
                return $this->redirect()->toUrl("cateList?material_catalog_id=$id");
            }
        }
    }

    public function reportListAction()
    {
        return new ViewModel();
    }

    public function ajaxReportListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        $material_name = isset($request['material_name']) ? $request['material_name'] : '';
        $material_code = isset($request['material_code']) ? $request['material_code'] : '';
        
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'material_name' => $material_name,
            'material_code' => $material_code,
            'page' => $page
        );
        
        $paginator = $this->getMaterialReportTable()->find($query);
        
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        
        return $viewModel;
    }

    public function addReportAction()
    {
        $material = $this->getMaterialTable()->findAll();
        
        return new ViewModel(array(
            'material' => $material
        ));
    }

    public function insertReportAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            unset($data['file']);
            $result = $this->getMaterialReportTable()->create($data);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("保存成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'reportList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("保存失败！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'addReport'
                ));
            }
        }
    }

    public function editReportAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['monitor_report_id'];
        $report = $this->getMaterialReportTable()->findOne($id);
        $material = $this->getMaterialTable()->findAll();
        
        return new ViewModel(array(
            'report' => $report,
            'material' => $material
        ));
    }

    public function updateReportAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            
            $data1 = array(
                'monitor_report_id' => '',
                'file' => ''
            );
            $report_data = array_diff_key($data, $data1);
            
            (int) $id = $data['monitor_report_id'];
            $result = $this->getMaterialReportTable()->updateById($report_data, $id);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/material', array(
                    'action' => 'reportList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                
                return $this->redirect()->toUrl("reportList?monitor_report_id=$id");
            }
        }
    }

    public function removeReportAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['monitor_report_id'];
        
        if ($id == 0) {
            $this->flashMessenger()->addErrorMessage("参数有误，请重新删除");
            return $this->redirect()->toRoute('crm/material', array(
                'action' => 'reportList'
            ));
        }
        
        $result = $this->getMaterialReportTable()->remove($id);
        
        if ($result) {
            
            $this->flashMessenger()->addSuccessMessage("删除成功！");
            return $this->redirect()->toRoute('crm/material', array(
                'action' => 'reportList'
            ));
        } else {
            
            $this->flashMessenger()->addErrorMessage("删除失败！");
            return $this->redirect()->toRoute('crm/material', array(
                'action' => 'reportList'
            ));
        }
    }
}